<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <template #header>
        <el-button type="primary" icon="ele-Plus" @click="dialogFormVisible = true">添加</el-button>
        <el-dialog v-model="dialogFormVisible" title="添加品牌" width="40%">
          <el-form label-width="100px">
            <el-form-item label="品牌名称" style="width: 70%">
              <el-input />
            </el-form-item>
            <el-form-item label="品牌Logo">
              <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                  <Plus />
                </el-icon>
                <template #tip>
                  <div class="el-upload__tip">上传图片必须是jpg 并且不能大于2M</div>
                </template>
              </el-upload>
            </el-form-item>
          </el-form>

          <template #footer>
            <span class="dialog-footer">
              <el-button @click="dialogFormVisible = false">关闭</el-button>
              <el-button type="primary" @click="dialogFormVisible = false">
                确定
              </el-button>
            </span>
          </template>
        </el-dialog>

      </template>
      <el-table :data="tabelData" border stripe>
        <el-table-column type="index" width="80" label="序号" align="center" />
        <el-table-column label="品牌名称" prop="tmName" />
        <el-table-column label="品牌logo" prop="logoUrl">
          <!-- 表格作用域插槽的作用域数据row代表当前行完整数据 $index代表当前数据的下标 -->
          <template v-slot="{ row }">
            <el-image style="width: 60px" :src="row.logoUrl" fit="fill" :lazy="true"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default>
            <el-button type="warning" size="small" icon="ele-Edit">修改</el-button>
            <el-button type="danger" size="small" icon="ele-Delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:page-size="limit" v-model:current-page="page" :page-sizes="[3, 5, 7, 9]" :pager-count="5"
        layout="prev, pager, next,sizes" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
  
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  name: "Trademark",
});
</script>
  
<script setup lang="ts">
import { ref, toRefs, watch } from "vue";
import type { TrademarkDataType } from "@/api/user"
import { reqUestList } from "@/api/user"
import { onMounted } from "vue"
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'

interface pageDateType {
  page: number;
  limit: number;
  total: number;
}

// 表格数据
const tabelData = ref<TrademarkDataType[]>([]);
//获取品牌列表数据请求函数
const getTrademarkList = async () => {
  try {
    const re = await reqUestList({
      page: page.value,
      limit: limit.value,
    });
    // console.log(re)
    tabelData.value = re.records;
    total.value = re.total
  } catch (e) {
    return Promise.reject(e)
  }
};

onMounted(() => {
  // 获取品牌列表数据
  getTrademarkList();
});

// 默认分页数据
const pageDate = ref<pageDateType>({
  page: 1,
  limit: 5,
  total: 0,
});
const { page, limit, total } = toRefs(pageDate.value);

watch([page, limit], () => {
  getTrademarkList();
});

const dialogFormVisible = ref(false)

const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>